<?php
/**
 * CRMLite Taxonomy Class
 *
 * @final
 * @package     Crmlite_Taxonomy
 * @version     $Id: Taxonomy.php 55 2007-09-09 12:44:28Z etng2004 $
 * @copyright   2006-2007 ETE.CN
 * @author      Yi BO  <etng2004@gmail.com>
 * @license     PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
class Crmlite_Taxonomy
{
    /**
     * 数据库操作句柄
     *
     * @var Zend_Db_Adapter_Abstract
     */
    protected $db = null;
    protected static $instance = null;
    protected $tbl_terms = 'terms';
    protected $tbl_term_term_taxonomy = 'term_taxonomy';
    protected $tbl_relationships = 'term_relationships';

    protected $options = array(
    'debug'         => false,
    );

    protected $taxonomies = array();
    /**
     * 初始化对象
     *
     * @param Zend_Db_Adapter_Abstract $db_adapter  数据库操作句柄
     * @param array $tables                         关系表格名称
     * @param array $config                         其他配置
     */
    public function __construct(Zend_Db_Adapter_Abstract $db_adapter, array $tables = array(), array $options = array())
    {
        $this->db = $db_adapter;
        /**
         * 合并参数
         */
        $this->options = array_merge($this->options, $options);
        /**
         * 设置操作的数据表
         */
        foreach ($tables as $name => $table)
        {
            $property_name = 'tbl_' . $name;
        	$this->$property_name = $table;
        }
    }
    public function getProperties()
    {
    	return get_object_vars($this);
    }
    public function taxonomyExist($name)
    {
    	return isset($this->taxonomies[$name]);
    }
    public function registerTaxonomy($name, $object, $hierarchical = false, $update_count_callback ='')
    {
        if($this->taxonomyExist($name))return false;
        $this->taxonomies[$name] = array(
        'name'  => $name,
        'object'  => $object,
        'hierarchical'  => $hierarchical,
        'update_count_callback'  => $update_count_callback,
        );
        return true;
    }

    public function getTerms($term)
    {

    }

    public function getObjects()
    {

    }
}

/**
 * Unit Testing
 */

if(basename(__FILE__) == $_SERVER['SCRIPT_FILENAME'])
{

    set_include_path(realpath('../../lib/') . PATH_SEPARATOR . get_include_path());
    require_once 'Zend/Loader.php';
    Zend_Loader::registerAutoload();
    $logger = new Zend_Log(new Zend_Log_Writer_Stream( __FILE__ . '.debug.log' ));
	$logger->info(__FILE__);

    $db = Zend_Db::factory('PDO_MYSQL', array(
    'host'  => 'localhost',
    'username'  => 'username',
    'password'  => 'password',
    'dbname'  => 'crmlite',
    'charset'  => 'utf8',
    ));
    $db->query('SET NAMES UTF8');

    $tables = array();
    $options = array();
    foreach ($tables as $table)
    {
    	$db->query('TRUNCATE TABLE ' . $table);
    }
    $taxonomy = new Crmlite_Taxonomy($db, $tables, $options);

    $taxonomy->registerTaxonomy('category', 'Article', true);
    $taxonomy->registerTaxonomy('link_category', 'Link');
    $taxonomy->registerTaxonomy('article_tag', 'Article');

    $category_taxonomy = '?';
//    $category_taxonomy->add
    var_dump($taxonomy->getProperties());

}